﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

using System.Configuration;

namespace GPCSC.Web.WBWZ
{
    public partial class SendEmail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string fjaddress = this.sjaddress.Value;//收件人地址
                string myaddre = ConfigurationManager.AppSettings["emailadd"];//我的邮箱地址
                string myname = ConfigurationManager.AppSettings["emailuser"];//我的用户名
                string mypass = ConfigurationManager.AppSettings["emailpas"];//我的邮箱密码

                string ips = "127.0.0.1";//主机ip

                //GPCSC.Web.SendEmail se = new Web.SendEmail();
                //se.
                DateTime t = DateTime.Now;
                String Subject = this.sjzhuti.Value;
                String body = this.sjnr.Value + "\n" + "发件人邮箱地址为：" + this.fjaddress.Value + "\n";
                String FromEmail = ConfigurationManager.AppSettings["emailadd"];
                String ToEmail = this.sjaddress.Value;


                sendMails(myaddre, fjaddress, body, Subject, myaddre, mypass, ips);
                Response.Write("<script>alert('邮件发送成功！')</script>");
            }
            catch (Exception ee)
            {
                throw ee;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="acpUserMail">发件人地址</param>
        /// <param name="ToMails">收件人地址</param>
        /// <param name="message">内容</param>
        /// <param name="title">主题</param>
        /// <param name="username">我的用户名</param>
        /// <param name="password">我的密码</param>
        /// <param name="smtp">主机IP地址</param>
        public void sendMails(string acpUserMail, string ToMails, string message, string title, string username, string password, string smtp)
        {



            MailMessage mail = new MailMessage();

            MailAddress add = new MailAddress(acpUserMail);
            //foreach (string tomail in ToMails)
            //{
            mail.To.Add(new MailAddress(ToMails));
          
            //}
            mail.From = add;

            mail.Subject = title; //主题
            mail.Body = message;  //内容
            mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");

            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.High;

            SmtpClient client = new SmtpClient();
            
            //设置用于 SMTP 事务的主机的名称，填IP地址也可以了
            client.Host = "123.58.178.204";
            //设置用于 SMTP 事务的端口，默认的是 25
           // client.Port = 25;
            client.UseDefaultCredentials = false;
            
            //这里才是真正的邮箱登陆名和密码，比如我的邮箱地址是 hbgx@hotmail， 我的用户名为 hbgx ，我的密码是 xgbh
            client.Credentials = new System.Net.NetworkCredential(username, password);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            //都定义完了，正式发送！
            client.Send(mail);
           
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //jmail.Message Jmail = new jmail.Message();

            //DateTime t = DateTime.Now;
            //String Subject = this.sjzhuti.Value;
            //String body = this.sjnr.Value + "\n" + "发件人邮箱地址为：" + this.fjaddress.Value+"\n";
            //String FromEmail = ConfigurationManager.AppSettings["emailadd"];
            //String ToEmail = this.sjaddress.Value;
            ////Silent属性：如果设置为true,JMail不会抛出例外错误. JMail. Send( () 会根据操作结果返回true或false 
            //Jmail.Silent = true;
            ////Jmail创建的日志，前提loging属性设置为true 
            //Jmail.Logging = true;
            ////字符集，缺省为"US-ASCII" 
            //Jmail.Charset = "GB2312";
            ////信件的contentype. 缺省是"text/plain"） : 字符串如果你以HTML格式发送邮件, 改为"text/html"即可。 
            //Jmail.ContentType = "text/html";
            ////添加收件人 
            //Jmail.AddRecipient(ToEmail, "", "");
            //Jmail.From = FromEmail;
            ////发件人邮件用户名 
            //Jmail.MailServerUserName = ConfigurationManager.AppSettings["emailuser"];
            ////发件人邮件密码 
            //Jmail.MailServerPassWord = ConfigurationManager.AppSettings["emailpas"];
            ////设置邮件标题 
            //Jmail.Subject = Subject;
            ////邮件添加附件,(多附件的话，可以再加一条Jmail.AddAttachment( "c:　　est.jpg",true,null);)就可以搞定了。［注］：加了附件，讲把上面的Jmail.ContentType="text/html";删掉。否则会在邮件里出现乱码。 
            ////Jmail.AddAttachment("c:　　est.jpg", true, null);
            ////邮件内容 
            //Jmail.Body = body + t.ToString();
            ////Jmail发送的方法 
            //Jmail.Send("smtp.163.com", false);
            //Jmail.Close();
            //Response.Write("<script>alert('邮件发送成功！')</script>");

        }

    }
}